package com.mercury.share.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mercury.share.entity.Share;
import com.mercury.share.entity.ShareComments;
import com.mercury.share.entity.ShareImgs;
import com.mercury.share.entity.vo.ShareInfo;
import com.mercury.share.mapper.ShareMapper;
import com.mercury.share.service.ShareCommentsService;
import com.mercury.share.service.ShareImgsService;
import com.mercury.share.service.ShareService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tong
 * @since 2021-11-22
 */
@Service
public class ShareServiceImpl extends ServiceImpl<ShareMapper, Share> implements ShareService {

    @Autowired
    private ShareCommentsService commentsService;

    @Autowired
    private ShareImgsService imgsService;

    @Override
    public List<ShareInfo> getShareList() {
        List<ShareInfo> shareList = baseMapper.getShareInfo();
        for (ShareInfo shareInfo : shareList) {
            shareInfo.setCount(commentsService.getCount(shareInfo.getId()));
            shareInfo.setPicList(imgsService.getList(shareInfo.getId()));
        }
        return shareList;
    }

    @Override
    public void addShare(ShareInfo shareInfo) {
        Share share = new Share();
        BeanUtils.copyProperties(shareInfo,share);
        share.setDeleted(false);
        baseMapper.insert(share);
        List<ShareImgs> picList = shareInfo.getPicList();
        for (ShareImgs shareImg : picList) {
            shareImg.setShareId(share.getId());
            imgsService.save(shareImg);
        }
    }

    /**
     * @date 2021/11/22 14:47
     * @params id
     * @return
     * @description 获得该id对应的分享详情
    */
    @Override
    public ShareInfo getShareInfo(int id) {
        return baseMapper.getShareInfoById(id);
    }

    @Override
    public Boolean isPersonal(int shareId, String userId) {
        QueryWrapper<Share> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("id",shareId);
        int count = baseMapper.selectCount(wrapper);
        if(count == 0) return false;
        else return true;
    }
}
